package com.hqj.client.controller;

import com.hqj.client.entity.BlogsCollect;
import com.hqj.client.service.BlogsCollectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 黄啟军
 * @version 1.0
 * @date 2020/5/16/016
 */
@Api(tags = "博客收藏表")
@RestController
@RequestMapping("/blogsCollect")
public class BlogsCollectController {

    @Resource
    private BlogsCollectService blogsCollectService;
    /***
     * BlogsCollect分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @ApiOperation(value = "BlogsCollect分页查询",notes = "分页查询BlogsCollect方法详情",tags = {"BlogsCollectController"})
    @GetMapping(value = "/search/{page}/{size}" )
    public String findPage(@PathVariable  int page,
                                                 @PathVariable  int size,
                                                 Model model){
        //调用BlogsCollectService实现分页查询BlogsCollect
       List<BlogsCollect> pageInfo = blogsCollectService.findPage(page, size);
       if (!pageInfo.isEmpty()){
           model.addAttribute("msg",pageInfo);
       }
        return "pageInfo";
    }
    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @ApiOperation(value = "BlogsCollect根据ID删除",notes = "根据ID删除BlogsCollect方法详情",tags = {"BlogsCollectController"})
    @DeleteMapping(value = "delete/{id}" )
    public String delete(@PathVariable Long id,Model model){
        //调用BlogsCollectService实现根据主键删除
        int i = blogsCollectService.delete(id);
        if(i>0){
            model.addAttribute("msg","删除成功");
            return "/page/BlogIndex";
        }
        return "/page/BlogIndex";

    }

    /***
     * 修改BlogsCollect数据
     * @param blogsCollect
     * @param id
     * @return
     */
    @ApiOperation(value = "BlogsCollect根据ID修改",notes = "根据ID修改BlogsCollect方法详情",tags = {"BlogsCollectController"})
    @PutMapping(value="update/{id}")
    public String update(@RequestBody @ApiParam(name = "BlogsCollect对象",value = "传入JSON数据",required = false) BlogsCollect blogsCollect,
                         @PathVariable Long id,Model model){
        //设置主键值
        blogsCollect.setId(id);
        //调用BlogsCollectService实现修改BlogsCollect
        int i = blogsCollectService.update(blogsCollect);
        if (i>0){
            model.addAttribute("msg","修改成功");
            return "/page/BlogIndex";
        }
        return "/page/BlogIndex";
    }

    /***
     * 新增BlogsCollect数据
     * @param blogsCollect
     * @return
     */
    @ApiOperation(value = "BlogsCollect添加",notes = "添加BlogsCollect方法详情",tags = {"BlogsCollectController"})
    @PostMapping("/insert")
    public String insert(@RequestBody  @ApiParam(name = "BlogsCollect对象",value = "传入JSON数据",required = true) BlogsCollect blogsCollect,Model model){
        //调用BlogsCollectService实现添加BlogsCollect
        BlogsCollect blogsCollect1 = blogsCollectService.judgeCollect(blogsCollect.getType(), blogsCollect.getUserName());
        if (blogsCollect1!=null){
            model.addAttribute("msg","该内容已收藏");
            return "/page/BlogIndex";
        }
        int i = blogsCollectService.add(blogsCollect);
        model.addAttribute("msg","新增收藏");
        return "/page/BlogIndex";

    }

    /***
     * 根据ID查询BlogsCollect数据
     * @param id
     * @return
     */
    @ApiOperation(value = "BlogsCollect根据ID查询",notes = "根据ID查询BlogsCollect方法详情",tags = {"BlogsCollectController"})
    @GetMapping("findById/{id}")
    public String findById(@PathVariable Long id,Model model){
        //调用BlogsCollectService实现根据主键查询BlogsCollect
        BlogsCollect blogsCollect = blogsCollectService.findById(id);
        if (blogsCollect!=null){
            model.addAttribute("mag",blogsCollect);
            return "/page/BlogIndex";
        }
        return "/page/BlogIndex";
    }
    /***
     * 查询BlogsCollect全部数据
     * @return
     */
    @ApiOperation(value = "查询所有BlogsCollect",notes = "查询所BlogsCollect有方法详情",tags = {"BlogsCollectController"})
    @GetMapping("/findAll")
    public String findAll(Model model){
        //调用BlogsCollectService实现查询所有BlogsCollect
        List<BlogsCollect> list = blogsCollectService.findAll();
        if (list.isEmpty()){
            model.addAttribute("msg","查询全部数据成功");
            return "/page/BlogIndex";
        }
        return "/page/BlogIndex";
    }

    @ApiOperation("判断是否收藏")
    @GetMapping("/judgeCollect")
    public  String judgeCollect(@RequestParam Integer typeId,
                                @RequestParam String userName,Model model){
        BlogsCollect blogsCollect = blogsCollectService.judgeCollect(typeId, userName);
        if (blogsCollect!=null){
            model.addAttribute("msg",blogsCollect);
            return"/page/BlogIndex";
        }
        return "/page/BlogIndex";
    }


}
